<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/13
 * Time: 16:21
 */
namespace app\wchat\controller;
use app\repository\code\Code;
use think\captcha\Captcha;
use think\Controller;
use think\Request;

class Login extends Controller
{
    /**
     * 企业登录页面
     * @return mixed
     */
    public function loginUI()
    {
        return $this->fetch('login');
    }

    /**
     * 登录提交
     * @param Request $request
     */
    public function login(Request $request){
        $user = $request->param('username');
        $pass = $request->param('pass');
        $code = $request->param('code');

        if($code){
            if(!($this->verif($code))){
                return ['code'=>0,'msg'=>'验证码错误'];
            }
        }
        $row = db('admin')->where('username',$user)->find();
        if($row){
            if($row['group_id'] != 6){
                return ['code'=>0,'msg'=>'只允许第三单位登录'];
            }
            if($row['pwd'] == md5($pass)){
                session('euid',$row['admin_id']);
                session('egid',$row['group_id']);
                return ['code'=>1,'msg'=>'登录成功'];
            }
        }
        return ['code'=>0,'msg'=>'用户名或密码错误'];
    }

    /**
     * 验证码
     * @return \think\Response
     */
    public function code(){
        (new Code())->make();
    }

    private function verif($code){
        return (new Code())->check($code);
    }

}